package com.turing.data.scripts.contract.filter

import com.turing.data.engine.TextUtils
import com.turing.data.engine.utils.ScriptsUtils
import com.turing.data.vo.out.review.DependParamsVO
import com.turing.data.vo.out.review.TaskElementVO
import org.apache.commons.lang.SerializationUtils
import org.springframework.util.CollectionUtils
import com.turing.common.util.FileUtils

class TerminationOfContract_30204_10_filter {

    static List< TaskElementVO.DrawResultVO > doConvert(DependParamsVO dependParams){

        def REGEX_TERMINATION_1 = '合同终止的情形'
        def REGEX_TERMINATION_2 = '资产管理计划终止的情形'
        /*def list = [ "期限届满而未",\
                     "协商一致决定",\
                     "依法解散",\
                     "依法撤销",\
                     "投资者少于二人",\
                     "备案",
                     "期限届满且不",
                    ]*/
        def list = []
        def signs = [".","、","．"]
        def REGEX_NUMBER_1 = '（1）'
        def REGEX_NUMBER_2 = '1、'
        def REGEX_NUMBER_3 = '1.'
        def REGEX_NUMBER_4 = '1．'
        def REGEX_NUMBER_5 = '(1)'
        List<TaskElementVO.DrawResultVO > trueValueSet = new ArrayList<>()
        List<TaskElementVO.DrawResultVO > resultVOs = ScriptsUtils.tryInitResult(dependParams)
        if (resultVOs!=null && resultVOs.size()>0) {
            for (int i = 0; i < resultVOs.size(); i++) {
                def drawResultVO = resultVOs.get(i)
                def childTaskSn = drawResultVO.childTaskSn
                if(null == childTaskSn || childTaskSn == ""){
                    continue
                }
                def start = drawResultVO.start
                def end = drawResultVO.end
                def text = drawResultVO.text
                text = text.replaceAll("\n","")
                def textPathMap = dependParams.textPathMap
                def rawPath = textPathMap.get(drawResultVO.childTaskSn)
                def rawText = FileUtils.loadJson(rawPath)
                def expandOffset = 0
                while( expandOffset < 150 && start > expandOffset ){
                    def expandString = rawText.substring(start-expandOffset,end)
                    if( ( expandString.contains(REGEX_TERMINATION_1) || expandString.contains(REGEX_TERMINATION_2)) ){
                        def startOffset = 0
                        def endOffset = 0
                        if(expandString.contains(REGEX_NUMBER_1)){
                            def index = expandString.indexOf(REGEX_NUMBER_1)
                            startOffset = index - expandOffset
                            endOffset = - ( expandString.length() + expandOffset - index )
                        }else if(expandString.contains(REGEX_NUMBER_2)){
                            def index = expandString.indexOf(REGEX_NUMBER_2)
                            startOffset = index - expandOffset
                            endOffset = - ( expandString.length() + expandOffset - index )
                        }else if(expandString.contains(REGEX_NUMBER_3)){
                            def index = expandString.indexOf(REGEX_NUMBER_3)
                            startOffset = index - expandOffset
                            endOffset = - ( expandString.length() + expandOffset - index )
                        }else if(expandString.contains(REGEX_NUMBER_4)){
                            def index = expandString.indexOf(REGEX_NUMBER_4)
                            startOffset = index - expandOffset
                            endOffset = - ( expandString.length() + expandOffset - index )  // index - expandOffset - expandString.length()
                        }else if(expandString.contains(REGEX_NUMBER_5)){
                            def index = expandString.indexOf(REGEX_NUMBER_5)
                            startOffset = index - expandOffset
                            endOffset = - ( expandString.length() + expandOffset - index )
                        }
                        for (int j = 2; j < 1000; j++) {
                            def REGEX_1 = '。' + j + '.'
                            def REGEX_2 = '；' + j + '.'
                            def REGEX_6 = ';' + j + '．'
                            def REGEX_7 = ';' + j + '.'
                            def REGEX_3 = '。' + j + '、'
                            def REGEX_4 = '。（' + j + '）'
                            def REGEX_5 = ';（' + j + '）'
						    def REGEX_8 = '。' + j + '．'
							def REGEX_9 = '。\\）' + j + '.'
							def REGEX_10 = '。\\/' + j + '.'
                            def expandOffset0 = 1
                            def replicationEndOffset = endOffset
                            while( expandOffset0 < 200 && rawText.length() > end + expandOffset0 + endOffset ){
                                def expandStr = rawText.substring(start+endOffset,end+expandOffset0+endOffset)
                                if( expandStr =~ REGEX_1 || expandStr =~ REGEX_2 || expandStr =~ REGEX_3 || expandStr =~ REGEX_4 || expandStr =~ REGEX_5 || \
								expandStr =~ REGEX_6 || expandStr =~ REGEX_7 || expandStr =~ REGEX_8 || expandStr =~ REGEX_9 || expandStr =~ REGEX_10 ){
                                    endOffset += expandOffset0
                                    break
                                }
                                expandOffset0 ++
                            }
                            if(replicationEndOffset == endOffset){
                                break
                            }
                        }

                        def REGEX_STOP = "。"
                        def expand = 1
                        while( expand < 250 && rawText.length() > end + expand + endOffset ){
                            def expandStr = rawText.substring(end+endOffset,end+expand+endOffset)
                            if( expandStr =~ REGEX_STOP ){
                                endOffset += expand
                                break
                            }
                            expand ++
                        }
                        def JSONTextMap = dependParams.JSONTextMap
                        def fileTextStr = ""
                        fileTextStr = JSONTextMap.get(drawResultVO.childTaskSn)
                        TaskElementVO.DrawResultVO objectNew = (TaskElementVO.DrawResultVO) SerializationUtils.clone(drawResultVO)
                        objectNew = TextUtils.correctPositionInfoByOffset(objectNew, startOffset, endOffset, fileTextStr, rawText)
                        objectNew.text = rawText.substring( start + startOffset, end + endOffset )
                        objectNew.businessValue = rawText.substring( start + startOffset, end + endOffset )
                        trueValueSet.add(objectNew)
                        return trueValueSet
                    }
                    expandOffset ++
                }
            }
        }

        if(!CollectionUtils.isEmpty(resultVOs) && resultVOs.size()>2){
            trueValueSet.add(resultVOs[0])
            trueValueSet.add(resultVOs[1])
            return trueValueSet
        }
        return  resultVOs

    }

    public static void main(String[] args) {

        def str = "的情形包括下列事项:1．资产管理合同存续期限届满而未延期的。2．经全体委托人、资产管理人、资产托管人书面协商一致决定终止的。）3．资产管理人被依法取消资产管理业务资格的或资产管理人依法解散、被依法撤销或被依法宣告破产的，且在六个月内没有新的管理人承接。/4．资产托管人依法撤销基金托管资格或依法解散、被依法撤销或被依法宣告破产的，且在六个月内没有新的托管人承接。5．本计划存续期间，持续五个工作日投资者少于二人。6．资产管理计划成立后，未通过证券投资基金业协会备"

        str = str.replaceAll("\n","")
        if(str =~ '。\\)3.'){
            println 1
        }

    }

}
